package com.one.common.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.one.common.R;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/23 13:46
 * Description：认证通用拍照控件
 **/
public class AuthCameraView extends LinearLayout {
    private TextView tvCamera;
    private LinearLayout llCamera;
    private ImageView ivShow, ivCamera;
    private OnClickListener listener;
    public AuthCameraView(Context context) {
        super(context, null);
    }

    public AuthCameraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (attrs == null) {
            return;
        }
        LayoutInflater.from(context).inflate(R.layout.layout_auth_camera_view, this);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.authCameraViewAttrs);
        String cameraText = ta.getString(R.styleable.authCameraViewAttrs_acv_camera_text);
        int cameraTextRes = ta.getResourceId(R.styleable.authCameraViewAttrs_acv_camera_text, -1);
        boolean isEnable = ta.getBoolean(R.styleable.authCameraViewAttrs_acv_is_enable, true);

        initView();

        setCameraText(cameraTextRes);
        setCameraText(cameraText);
        setIsEnable(isEnable);

        ta.recycle();
    }

    public void initView(){
        llCamera = findViewById(R.id.ll_auth_camera);
        tvCamera = findViewById(R.id.tv_camera);
        ivShow = findViewById(R.id.iv_auth_show);
        ivCamera = findViewById(R.id.iv_camera);
        llCamera.setOnClickListener(v -> {
            if(listener!=null){
                listener.onClick(AuthCameraView.this);
            }
        });
    }

    public void setCameraText(String cameraText) {
        tvCamera.setText(cameraText);
    }

    public void setCameraText(int cameraTextRes) {
        if (cameraTextRes != -1) {
            tvCamera.setText(cameraTextRes);
        }
    }

    /**
     * 设置是否可以点击
     */
    public void setIsEnable(boolean isEnable){
        llCamera.setEnabled(isEnable);
        if(isEnable){
            ivCamera.setImageResource(R.mipmap.ic_camera_green);
            tvCamera.setTextColor(getResources().getColor(R.color.root_green));
        } else {
            ivCamera.setImageResource(R.mipmap.ic_camera_gray);
            tvCamera.setTextColor(getResources().getColor(R.color.hint_cccccc));
        }
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        listener = l;
    }


    public ImageView getIvShow() {
        return ivShow;
    }
}
